﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TL_Cloud.Core.Extensions;
using TL_Cloud.Core.Interfaces;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.RepositoryService.BookShop;
using TL_Cloud.ViewModels.BookShop.BKBuyInfo;
using TL_Cloud.ViewModels.BookShop.BKCollect;
using TL_Cloud.ViewModels.BookShop.BKComment;
using TL_Cloud.ViewModels.BookShop.BKDownLoadRecord;
using TL_Cloud.ViewModels.BookShop.BKPics;
using TL_Cloud.ViewModels.BookShop.BKRecommend;
using TL_Cloud.ViewModels.BookShop.BKStoreUrl;

namespace TL_Cloud.API.Controllers
{
    /// <summary>
    /// 图书存储地址信息
    /// </summary>
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class BKStoreUrlController : ControllerBase
    {
        private readonly ILogger logger;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IBKStoreUrlRepository _repository;
        //private readonly IBKInfoRepository _bookInfoReposotory;
        private readonly IUrlHelper _urlHelper;

        public BKStoreUrlController(
            ILogger<BKInfoController> logger,
            IBKStoreUrlRepository repository,
           // IBKInfoRepository bookInfoReposotory,
            IUnitOfWork unitOfWork
            )
        {

            this.logger = logger;
            this._repository = repository;
            this._unitOfWork = unitOfWork;
            //this._bookInfoReposotory = bookInfoReposotory;
        }
        /// <summary>
        /// 获取单个图书的存储地址
        /// </summary>
        /// <param name="id">图书编号</param>
        /// <returns></returns>
        [HttpGet("{id}", Name = "GetBKStoreUrl")]
        public async Task<IActionResult> Get(string id,string fields)
        {            
            
            var entity = await _repository.GetEntityByIdAsync(id);

            if (entity == null)
            {
                return NotFound();
            }
             
            var shapedentityResource = entity.ToDynamic(fields);     
            return Ok(shapedentityResource);
        }
        ///<summary>
        /// 更新图书存储地址
        /// </summary>
        /// <param name="id">图书编号</param>
        [HttpPut("{id}", Name = "UpdateBkStoreUrl")]
        public async Task<IActionResult> Update(string id, [FromBody] JObject obj)
        {

            Bk_StoreUrl storeInfo = await _repository.GetBookStoreInfo(id);
            var userinfo = this.GetUserInfo();
            if (storeInfo==null)
            {
                storeInfo = new Bk_StoreUrl();
                storeInfo.SrcUrl = obj["src"].ToString();
                storeInfo.BookId = id;
                storeInfo.Create(storeInfo.CreateUserId,storeInfo.CreateUserName);
                _repository.Add(storeInfo);
            }
            else
            {
                storeInfo.SrcUrl = obj["src"].ToString();
                storeInfo.ConvertStatus = 0;
                storeInfo.Modify(userinfo.Item1, userinfo.Item2);
                _repository.Update(storeInfo);
            }        

            if (!await _unitOfWork.SaveAsync())
            {
                throw new Exception($"Updating entity {id} failed when saving.");
            }
            return NoContent();
        }


        ///// <summary>
        ///// 删除图书的存储地址
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpDelete("{id}", Name = "DeleteBKStoreUrl")]
        //public async Task<IActionResult> Delete(string id)
        //{
        //    var post = await _repository.GetEntityByIdAsync(id);
        //    if (post == null)
        //    {
        //        return NotFound();
        //    }
        //    _repository.Delete(post);

        //    if (!await _unitOfWork.SaveAsync())
        //    {
        //        throw new Exception($"Deleting entity {id} failed when saving.");
        //    }

        //    return NoContent();
        //}
    }
}
